<?php

/**
 * Description of scatekeyword
 *
 * @author libaoling
 * @time 2014-12-17
 */
class O2okeywordModel extends BaseModel
{
    var $table  = 'o2o_keyword';
    var $prikey = 'keyword_id';
    var $_name  = 'osokeyword';
    var $_autov = array(
        'recom_id' => array(
            'required'  => true,
            'filter'    => 'intval',
        ),
    );
    public $_relation  =   array(
        'has_o2okeywordlang' => array(
            'model'         => 'o2okeywordlang',
            'type'          => HAS_MANY,
            'foreign_key'   => 'keyword_id',
            'dependent'     => true
        ),
    );
    
    function get_by_id($keyword_id)
    {
        $result = $this->findAll(array(
                'conditions' => "keyword_id = '$keyword_id'",
                'include' => array('has_o2okeywordlang' => array( 'index_key' => 'lang')),
            ));
        return $this->remake_keywords($result);
    }
    
    public function remake_keywords($keywords)
    {
        $result = $keywords;
        foreach ($keywords as $key => $keyword)
        {
            if(isset($keyword['o2o_keyword_lang']))
            {
                foreach ($keyword['o2o_keyword_lang'] as $lang => $value)
                {
                    $result[$key]['keyword'][$value['lang']] = $value['keyword'];
                    $result[$key]['keyword_'.$value['lang']] = $value['keyword'];
                }
                unset($result[$key]['o2o_keyword_lang']);
            }
        }
        return $result;
    }
}
